package com.example.dql.diary.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.transition.TransitionSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.dql.diary.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.img_login)
    ImageView imgLogin;
    @BindView(R.id.et_loginAccount)
    EditText etLoginAccount;
    @BindView(R.id.et_loginPwd)
    EditText etLoginPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    private int screenHeight = 0;//屏幕高度
    private int keyHeight = 0; //软件盘弹起后所占高度
    private float scale = 0.6f; //logo缩放比例
    private int height = 0;
    private Float imgHeight;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

    @Override
    public void init() {
        imgHeight = imgLogin.getY();
        Log.e("OldimgHeight", String.valueOf(imgHeight));
        screenHeight = this.getResources().getDisplayMetrics().heightPixels; //获取屏幕高度
        Log.e("screenHeight", String.valueOf(screenHeight));
        keyHeight = screenHeight / 4;//弹起高度为屏幕高度的1/4
        TransitionSet mtransitionset=new TransitionSet();//制定过度动画set
        mtransitionset.setDuration(1000);
        getWindow().setExitTransition(mtransitionset);
        getWindow().setSharedElementExitTransition(mtransitionset);
        scrollView.addOnLayoutChangeListener(new ViewGroup.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int dist = llContent.getBottom() - bottom;
                imgHeight = imgLogin.getY();
                //打开键盘的时候
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
                    Log.e("up-oldBottom", String.valueOf(oldBottom));
                    Log.e("up-getBottom", String.valueOf(llContent.getBottom()));//原llContent的位置
                    Log.e("up-bottom", String.valueOf(bottom));
                    Log.e("this", "up------>" + (oldBottom - bottom));//ScrollView向上滑动的距离
                    Log.e("dist", String.valueOf(dist));//llContent向上移动的距离
                    Log.e("imgHeight", String.valueOf(imgHeight));
                    if (dist > 0) {
                        //第一个参数为 view对象，第二个参数为 动画改变的类型
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(llContent, "translationY", 0, -(dist - 43));
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        ImageChange(imgLogin, 0.6f, (0));//第三个值一般根据logo本身的位置来设置平移的距离
                    }

                }
                //收起键盘的时候
                else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
                    Log.e("this", "down------>" + (bottom - oldBottom));
                    Log.e("down-oldBottom", String.valueOf(oldBottom));
                    Log.e("down-getBottom", String.valueOf(llContent.getBottom()));
                    Log.e("down-bottom", String.valueOf(bottom));
                    Log.e("dist", String.valueOf(dist));
                    if ((llContent.getBottom() - oldBottom) > 0) {
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(llContent, "translationY", llContent.getTranslationY(), 0);
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        //键盘收回后，logo恢复原来大小，位置同样回到初始位置
                        if (dist < 0) {
                            ImageChangeBack(imgLogin, 0.6f);
                        }

                    }
                }
            }
        });
    }

    private void ImageChangeBack(View view, float scale) {
        //获取logo的坐标
        view.setPivotY((float) view.getHeight());
        view.setPivotX((float) (view.getWidth() / 2));//获取中点

        //属性动画（X轴缩放，Y轴缩放，Y轴平移）
        AnimatorSet mAnimatorSet = new AnimatorSet();
        ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", new float[]{scale, 1.0F});
        ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", new float[]{scale, 1.0F});
        Log.e("view.getTranslationY()", String.valueOf(view.getTranslationY()));
        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", new float[]{view.getTranslationY(), 0.0F});
        mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX);
        mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY);
        mAnimatorSet.setDuration(300L);
        mAnimatorSet.start();
    }

    private void ImageChange(View view, float scale, int dist) {
        view.setPivotY((float) view.getHeight());
        view.setPivotX((float) (view.getWidth() / 2));
        AnimatorSet mAnimatorSet = new AnimatorSet();
        ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", new float[]{1.0F, scale});
        ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", new float[]{1.0F, scale});
        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", new float[]{0.0F, -dist});
        mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX);
        mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY);
        mAnimatorSet.setDuration(300L);
        mAnimatorSet.start();
    }


    @OnClick({R.id.button_back, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button_back:
                finish();
                break;
            case R.id.btn_login:
                break;
        }
    }
}
